1. Field of the Invention
This invention relates to virtualization and the use of virtual machines in processors and computer systems and, more particularly, to switching between guests and host in a processor/computer system implementing virtualization.
2. Description of the Related Art
Virtualization has been used in computer systems for a variety of different purposes. For example, virtualization may be used to execute privileged software in a “container” to prevent the privileged software from directly accessing and/or making changes to at least some of the physical machine state without first being permitted to do so by a virtual machine manager (VMM) that controls the virtual machine. Such a container may prevent “buggy” or malicious software from causing problems on the physical machine. Additionally, virtualization may be used to permit two or more privileged programs to execute on the same physical machine concurrently. The privileged programs may be prevented from interfering with each other since access to the physical machine is controlled. Privileged programs may include operating systems, and may also include other software which expects to have full control of the hardware on which the software is executing. In another example, virtualization may be used to execute a privileged program on hardware that differs from the hardware expected by the privileged program.
Generally, virtualization of a processor or computer system may include providing one or more privileged programs with access to a virtual machine (the container mentioned above) over which the privileged program has full control, but the control of the physical machine is retained by the VMM. The virtual machine may include a processor (or processors), memory, and various peripheral devices that the privileged program expects to find in the machine on which it is executing. Each privileged program (and related software in some cases, such as the applications that execute on an operating system) may be referred to herein as a guest. Virtualization may be implemented in software (e.g. the VMM mentioned above) without any specific hardware virtualization support in the physical machine on which the VMM and its virtual machines execute. However, virtualization may be simplified and/or achieve higher performance if some hardware support is provided.
Both the VMM and the guests are executed by the processor(s) included in the physical machine. Accordingly, switching between execution of the VMM and the execution of guests occurs in the processor(s) over time. Particularly, the VMM schedules a guest for execution, and a switch to executing that guest is performed. At various points in time, a switch from executing a guest to executing the VMM also occurs so that the VMM can retain control over the physical machine (e.g. when the guest attempts to access a peripheral device, when a new page of memory is to be allocated to the guest, when it is time for the VMM to schedule another guest, etc.). A switch between a guest and the VMM (in either direction) is often referred to as a “world switch”.
Generally, the world switch involves saving all of the processor's state for the guest/VMM being switched away from, and restoring all of the processor's state for the guest/VMM being switched to. In some cases, saving/restoring all of the state may be necessary. For example, when a guest is being started for the first time, none of the corresponding processor state has been loaded and thus all of the processor state may be saved/loaded. However, in other cases, some of the processor state may not change from guest context to VMM context and vice-versa. In still other cases, a world switch may be brief (e.g. the VMM may execute briefly to service a simple guest exit for an intercepted event), and only a small amount of processor state for the VMM may be needed.